package com.itheima.service;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mapper.PmsProductMapper;
import com.itheima.mapper.SmsFlashPromotionProductRelationMapper;
import com.itheima.pojo.PmsProduct;
import com.itheima.pojo.SmsCouponProductRelation;
import com.itheima.pojo.SmsFlashPromotionProductRelation;
import com.itheima.vo.SmsFlashPromotionProduct;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class SmsFlashPromotionProductRelationImpl implements SmsFlashPromotionProductRelationApi{
    @Autowired
    private SmsFlashPromotionProductRelationMapper smsFlashPromotionProductRelationMapper;
    @Autowired
    private PmsProductMapper pmsProductMapper;
    /**
     * 批量选择商品添加关联
     * @param relationList
     * @return
     */
    @Override
    public void create(List<SmsFlashPromotionProductRelation> relationList) {
        for (SmsFlashPromotionProductRelation smsFlashPromotionProductRelation : relationList) {
            smsFlashPromotionProductRelationMapper.insert(smsFlashPromotionProductRelation);
        }
    }
    /**
     * 删除关联
     * @param id
     * @return
     */
    @Override
    public void deleteById(Integer id) {
        smsFlashPromotionProductRelationMapper.deleteById(id);
    }

    /**
     *分页查询不同场次关联及商品信息
     * @param flashPromotionId
     * @param flashPromotionSessionId 编号
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public IPage<SmsFlashPromotionProduct> list(Integer flashPromotionId, Integer flashPromotionSessionId, Integer pageNum, Integer pageSize) {
        IPage<SmsFlashPromotionProduct> pg=new Page<>(pageNum,pageSize);
        List<SmsFlashPromotionProduct> smsFlashPromotionProducts=new ArrayList<>();
        //先查询SmsFlashPromotionProductRelation
        LambdaQueryWrapper<SmsFlashPromotionProductRelation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SmsFlashPromotionProductRelation::getFlashPromotionId, flashPromotionId);
        wrapper.eq(SmsFlashPromotionProductRelation::getFlashPromotionSessionId,flashPromotionSessionId);
        //通过flashPromotionId,flashPromotionSessionId查询到SmsFlashPromotionProductRelation集合
        List<SmsFlashPromotionProductRelation> smsFlashPromotionProductRelations = smsFlashPromotionProductRelationMapper.selectList(wrapper);
        for (SmsFlashPromotionProductRelation smsFlashPromotionProductRelation : smsFlashPromotionProductRelations) {
            SmsFlashPromotionProduct smsFlashPromotionProduct=new SmsFlashPromotionProduct();
            //获取对应商品id
            Long productId = smsFlashPromotionProductRelation.getProductId();
            //通过商品id查询对应商品对象
            PmsProduct product = pmsProductMapper.selectById(productId);
            smsFlashPromotionProduct.setProduct(product);
            //到此smsFlashPromotionProduct对象封装完毕
            //每一个smsFlashPromotionProduct对象添加到集合中
            smsFlashPromotionProducts.add(smsFlashPromotionProduct);
        }
        pg.setRecords(smsFlashPromotionProducts);
        return pg;
    }

    /**
     * 修改关联信息
     * @param id
     * @param relation
     * @return
     */
    @Override
    public void updateById(Integer id, SmsFlashPromotionProductRelation relation) {
        relation.setId(Convert.toLong(id));
        smsFlashPromotionProductRelationMapper.updateById(relation);
    }

    /**
     * 获取管理商品促销信息
     * @param id
     * @return
     */
    @Override
    public SmsFlashPromotionProductRelation getRelation(Integer id) {
        return smsFlashPromotionProductRelationMapper.selectById(id);
    }


}
